package com.bhf.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.ImageObserver;
import java.io.File;

import javax.swing.ImageIcon;

import com.bhf.factory.BulletFactory;
import com.bhf.frame.MainFrame;

/**
 * 飞机类
 * 
 * @author 边宏飞
 * 
 */
public class Plane {
	// 飞机的宽度
	public static int PLANE_WIDTH = 40;
	public static int PLANE_HEIGHT = 60;

	private int left;
	private int height;

	private MainFrame frame;

	private boolean live;
	private boolean isStoping;

	private String img_path = "img/T50.png";

	private static Image imageIcon = null;

	// new javax.swing.ImageIcon(getClass().getResource("/i/b1.JPG"))
	public Plane(MainFrame frame) {
		this.frame = frame;
		this.live = true;
		String baseDir = System.getProperty("user.dir");
		String s = baseDir + File.separator + img_path;
		System.out.println(s);

		imageIcon = new ImageIcon(s).getImage();
		if (imageIcon == null) {
			System.out.println("没找到T50飞机图片");
		}
		// imageIcon = new
		// ImageIcon(getClass().getResource(img_path)).getImage();
		// PLANE_WIDTH=imageIcon.getWidth(new ImageObserverAdapter());
		// PLANE_HEIGHT=imageIcon.getHeight(new ImageObserverAdapter());
		PLANE_WIDTH = 50;
		PLANE_HEIGHT = 80;

		setCenter();
		// 启动发射炮弹的线程，从而不停地发射炮弹
		new Thread(new LauchBulletThread()).start();
	}

	private void setCenter() {
		this.left = frame.getWidth() / 2;
		this.height = frame.getHeight() - 100;
	}

	/**
	 * 飞机发出一枚炮弹
	 * 
	 * @return
	 */
	public Bullet lauchBullet() {
		if (!live) {
			return null;
		}
		if (isStoping) {
			return null;
		}
		Bullet b = BulletFactory.createBullet(this);
		 Bullet b1 = BulletFactory.createBullet_left(this);
		 Bullet b2 = BulletFactory.createBullet_right(this);

		// 注册监听器
		b.addBulletListener(frame);
		 b1.addBulletListener(frame);
		 b2.addBulletListener(frame);

		// 注册监听器
		frame.getBullets().add(b);
		 frame.getBullets().add(b1);
		 frame.getBullets().add(b2);

		// /////////////////////

		return b;
	}

	/**
	 * 取得我方飞机的区域 可以用public Rectangle union(Rectangle r)产生更好的区域
	 * 
	 * @return
	 */
	public Rectangle getRectangle() {
		int f_w = (int) (PLANE_WIDTH * 0.2); // /
		int f_h = (int) (PLANE_HEIGHT * 0.35); // /
		Rectangle rec = new Rectangle(left + f_w, height + f_h, (int) (PLANE_WIDTH * 0.7), (int) (PLANE_HEIGHT * 0.5));
		return rec;
	}

	/**
	 * 画出自己
	 * 
	 * @param g
	 */
	public void draw(Graphics g) {
		g.setColor(Color.BLUE);
		// g.fillRect(left, height, PLANE_WIDTH, PLANE_HEIGHT);
		g.drawImage(imageIcon, left, height, PLANE_WIDTH, PLANE_HEIGHT, new ImageObserverAdapter());

	}

	/**
	 * 随着鼠标的移动，也移动飞机的位置
	 * 
	 * @param e
	 */
	public void mouseMoved(MouseEvent e) {
		// 把鼠标弄到飞机中心
		int x = e.getX() - (Plane.PLANE_WIDTH / 2);
		int y = e.getY() - (Plane.PLANE_HEIGHT / 2);

		left = x;
		height = y;

		if (left >= MainFrame.WIDTH - Plane.PLANE_WIDTH / 2) {
			left = MainFrame.WIDTH - Plane.PLANE_WIDTH / 2;
		}

		if (height >= MainFrame.HEIGTH - Plane.PLANE_HEIGHT) {
			height = MainFrame.HEIGTH - Plane.PLANE_HEIGHT;
		}
	}

	public int getLeft() {
		return left;
	}

	public void setLeft(int left) {
		this.left = left;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public MainFrame getFrame() {
		return frame;
	}

	public boolean isLive() {
		return live;
	}

	public void setLive(boolean live) {
		this.live = live;
	}

	/**
	 * 连续发射炮弹的线程
	 * 
	 * @author 边宏飞
	 * 
	 */
	class LauchBulletThread implements Runnable {
		public void run() {
			while (true) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Bullet b = lauchBullet();
			}
		}
	}

	public void stopFire() {
		isStoping = true;
	}

	public void reStartFire() {
		isStoping = false;
	}

	public void move(int keyCode) {
		int step = 20;
		switch (keyCode) {
		case KeyEvent.VK_RIGHT:
			move((1) * step, 0);
			// moveRight();
			break;
		case KeyEvent.VK_LEFT:
			move((-1) * step, 0);
			// moveLeft();
			break;
		case KeyEvent.VK_UP:
			move(0, (-1) * step);
			// moveUp();
			break;
		case KeyEvent.VK_DOWN:
			move(0, (1) * step);
			// moveDown();
			break;
		default:
			break;
		}
	}

	private void move(int i, int j) {
		left += i;
		height += j;
		if (left >= MainFrame.WIDTH - Plane.PLANE_WIDTH) {
			left = MainFrame.WIDTH - Plane.PLANE_WIDTH;
		} else if (left < -Plane.PLANE_WIDTH / 3) {
			left = (-1) * (Plane.PLANE_WIDTH / 3);
		}
		if (height >= MainFrame.HEIGTH - Plane.PLANE_HEIGHT) {
			height = MainFrame.HEIGTH - Plane.PLANE_HEIGHT;
		} else if (height < 0) {
			height = 0;
		}
	}

	class ImageObserverAdapter implements ImageObserver {
		@Override
		public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
			return false;
		}

	}

}
